<template>
    <div class="">
      <Head>求职登记</Head>
      <van-loading v-if="loading" size="24px" class="loading">加载中...</van-loading>
      <div v-else>
        <div class="head">
          <img src="../../assets/images/jobRegister_banner.png" alt="">
          <span class="word">找好工作 上{{baseData.sitename}}</span>
        </div>
        <div class="des">---请认真填写下面内容，为您免费推荐好工作---</div>

        <div class="vanForm">
          <div class="vanRow" v-for="(item,index) of formList" :key="index">
            <span class="vanTitle" :class="item.is_must == 1 ? 'vanTitleRequest' : ''">{{ item.field_name }}</span>
            <!-- input -->
            <div v-if="item.field_type == 1  && item.field_way == 1">
              <van-field v-model="form[item.field_alias]"
                         :maxlength="item.field_length > 0 ? item.field_length: '3000'"
                         :placeholder="item.field_remark"/>
            </div>
            <!-- 验证码 -->
            <div v-if="item.field_type == 1 && item.field_way == 2">
              <div class="codeBox" style="margin-bottom: 20px;" >
                <van-field v-model="form[item.field_alias]"
                           type="number"
                           :maxlength="item.field_length > 0 ? item.field_length: '3000'"
                           :placeholder="item.field_remark" />
              </div>
              <span class="vanTitle" :class="item.is_must == 1 ? 'vanTitleRequest' : ''">验证码</span>
              <div class="codeBox">
                <van-field v-model="form.verify_code" placeholder="请输入验证码" type="number" maxlength="8"/>
                <span class="codeButton" @click="sendSms">{{ $store.state.sendSmsBtnText }}</span>
              </div>
            </div>
            <!-- 单选 -->
            <div v-if="item.field_type == 2" style="margin-top: 10px">
              <van-radio-group v-model="form[item.field_alias]" class="flex">
                <van-radio v-for="(radio,radioIndex) of item.field_value"
                           :key="radioIndex"
                           :name="radio.field_value"
                           class="right40">
                  <span class="radioWord" :class="form[item.field_alias] == radio.field_value ?  'radioActiveWord' : ''">{{ radio.field_value }}</span>
                  <template #icon="props">
                    <span class="radioIcon" :class="props.checked ? 'radioActiveIcon' : ''"></span>
                  </template>
                </van-radio>
              </van-radio-group>
            </div>
            <!-- 多选 -->
            <div v-if="item.field_type == 3">
              <van-checkbox-group v-model="form[item.field_alias]" class="vanTagBox">
                <van-checkbox v-for="(check,checkIndex) of item.field_value"
                              :key="checkIndex"
                              class="vanTag"
                              :name="check.field_value">
                  <template #icon="props">
                    <div class="vanTagInner" :class="props.checked ? 'vanTagActive' : ''"><img v-if="props.checked ? true : false" src="../../assets/images/jobRegister_icon.png" class="vanTagImg">{{ check.field_value }}</div>
                  </template>
                </van-checkbox>
              </van-checkbox-group>
            </div>
            <!-- 下拉 -->
            <div v-if="item.field_type == 4">
              <van-field v-model="form[item.field_alias]"
                         is-link
                         readonly
                         :placeholder="item.field_remark"
                         @click="showPickerFun"/>
              <van-popup v-model:show="showPicker" position="bottom">
                <van-picker
                  show-toolbar
                  :columns="item.field_value"
                  value-key="field_value"
                  @confirm="onConfirm($event,item.field_alias)"
                  @cancel="showPicker = false"
                />
              </van-popup>
            </div>
            <!-- 多行文本 -->
            <div v-if="item.field_type == 5">
              <van-field v-model="form[item.field_alias]"
                         rows="2"
                         autosize
                         type="textarea"
                         :placeholder="`${item.field_remark}(${item.field_length}字)`"/>
            </div>
          </div>
        </div>
        <div class="button" @click="submit">提交信息</div>

        <div class="successBox" v-if="success">
          <div class="successInner">
            <img class="successIcon" src="../../assets/images/jobRegister_icon_1.png" alt="">
            <span class="successWord">提交成功</span>
            <span>我们将及时为您推荐好工作！</span>
            <div class="successButton">
              <span class="successGo" @click="goWork">去找工作</span>
              <span class="successHome" @click="goHome">返回首页</span>
            </div>
          </div>
          <div class="successCode">
            <img :src="$store.state.config.wechat_qrcode" alt="" class="successCodeImg">
            <div class="successCodeWord">
              <span class="words">扫码进入公众号，了解更多!</span>
              <span>电话：{{baseData.contact_tel}}</span>
            </div>
          </div>
        </div>

        <Captcha ref="captcha"></Captcha>
      </div>

    </div>
</template>

<script>
import http from '@/utils/http'
import api from '@/api'
import Captcha from '@/components/captcha/index'
import wxshare from '../../assets/js/share'
export default {
  name: 'jobRegister',
  components: {
    Captcha
  },
  data () {
    return {
      regularMobile: /^13[0-9]{9}$|14[0-9]{9}$|15[0-9]{9}$|18[0-9]{9}$|17[0-9]{9}$|16[0-9]{9}$|19[0-9]{9}$/,
      formList: [],
      baseData: [],
      form: {},
      showPicker: false,
      success: false,
      submitFlag: false,
      loading: true,
      sendSmsLimit: false
    }
  },
  created () {
    this.init()
    wxshare({}, 'job_register', location.href)
  },
  methods: {
    init () {
      http.get(api.getRegisterViewData).then((res) => {
        const { code, data, message } = res
        if (code == '200') {
          this.formList = data.field_list
          this.baseData = data.base_data
          this.loading = false
        } else {
          this.$toast.fail(message)
        }
      }).catch(() => {})
    },
    submit () {
      let formArr = this.formList
      for (let i = 0; i < formArr.length; i++) {
        if (formArr[i].is_must == 1) {
          if (!this.form[formArr[i].field_alias]) {
            // 1|文本框,2|单选框,3|复选框,4|下拉框,5|多行文本
            if (formArr[i].field_type == 1 || formArr[i].field_type == 5) {
              this.$notify(`请填写${formArr[i].field_name}`)
            } else {
              this.$notify(`请选择${formArr[i].field_name}`)
            }
            return false
          }
        }
      }
      if (!this.form.verify_code) {
        this.$notify(`请填写验证码`)
      }

      http.post(api.saveRegisterData, this.form).then((res) => {
        const { code, message } = res
        if (code == '200') {
          this.success = true
        } else {
          this.$toast.fail(message)
        }
      }).catch(() => {})
    },
    showPickerFun () {
      this.showPicker = true
    },
    onConfirm (e, name) {
      this.form[name] = e.field_value
      this.showPicker = false
    },
    // 发送验证码 zdq验证码重复点击修改
    sendSms () {
      if (this.$store.state.sendSmsBtnDisabled) {
        return false
      }
      if (!this.form.contact_way) {
        this.$notify('请输入手机号')
        return false
      }
      if (!this.regularMobile.test(this.form.contact_way)) {
        this.$notify('手机号格式不正确')
        return false
      }
      if (this.sendSmsLimit) {
        return false
      }
      this.sendSmsLimit = true
      this.$refs.captcha.show(res => {
        this.$store
          .dispatch('sendSmsFun', {
            url: api.sendsms_auth_mobile_nocheck,
            mobile: this.form.contact_way,
            captcha: res
          })
          .then(response => {
            this.sendSmsLimit = false
            if (response.code === 200) {
              this.$notify({
                type: 'success',
                message: this.$store.state.sendSmsMessage
              })
            } else {
              this.$notify(this.$store.state.sendSmsMessage)
            }
          })
      })
    },

    //   去找工作
    goWork () {
      this.$router.push('/joblist')
    },
    goHome () {
      this.$router.push('/index')
    }
  }
}
</script>

<style lang="scss" scoped>
.head{
  position: relative;
  left: 0;
  top: 0;
  font-size: 12px;
  font-weight: bold;
  color: #FFFAF5;
  .word{
    position: absolute;
    top: 92px;
    left: 38px;
  }
  img{
    width: 100%;
  }
}
  .des{
    width: 100%;
    background: #fff;
    padding: 9px 0;
    font-size: 13px;
    font-weight: 500;
    color: #FF3F15;
    text-align: center;
    margin-top: -25px;
    border-radius: 16px 16px 0 0;
    transform: rotate(360deg);
  }
  .button{
    margin: 0 auto 20px;
    width: 290px;
    height: 40px;
    background: linear-gradient(-90deg, #FF0C2E, #FF6A00);
    border-radius: 20px;
    font-size: 15px;
    font-weight: bold;
    color: #FFFFFF;
    text-align: center;
    line-height: 38px;
  }

  .vanForm{
    padding: 20px;
    box-sizing: border-box;
  }
  .vanRow{
    margin-bottom: 20px;
    font-size: 12px;
  }
  .vanTitle{
    font-size: 14px;
    font-weight: 500;
    color: #454545;
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    justify-content: flex-start;
    margin-bottom: 6px;
  }
  .vanTitleRequest{
    &:before{
      content: "*";
      color: #FF0000;
      margin-right: 3px;
    }
  }
  .radioIcon{
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 1px solid #B0B0B0;
    border-radius: 50%;
  }
  .radioActiveIcon{
    border: 5px solid #FF3F15;
  }
  .radioWord{
    font-size: 13px;
    font-weight: 500;
    color: #7E7E7E;
  }
  .radioActiveWord{
    color: #FF3F15;
  }

.van-cell{
  width: 100%;
  background: #F7F7F7;
  border-radius: 4px;
  padding: 8px 12px;

}
  .flex{
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    flex-wrap: wrap;
  }
  .right40{
    margin-right: 40px;
  }

  .vanTagBox{
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    flex-wrap: wrap;
    margin-bottom: -10px;
  }
  .vanTag{
    margin: 0 10px 10px 0;
    overflow: hidden;
  }
  .vanTagInner{
    background: #F7F7F7;
    border-radius: 4px;
    padding: 10px 16px;
    box-sizing: border-box;
    border: 1px solid transparent;
    font-size: 12px;
    font-weight: 500;
    color: #7A7A7A;
    position: relative;
    overflow: hidden;
    line-height: 1;
  }
  .vanTagImg{
    position: absolute;
    right: -1px;
    bottom: -1px;
    width: 14px;
    height: 14px;
  }
  .vanTagActive{
    border: 1px solid #FF3F15;
    color: #FF3F15;
  }
  .codeBox{
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
  }
  .codeButton{
    width: 104px;
    height: 38px;
    border: 1px solid #FF0000;
    border-radius: 4px;
    text-align: center;
    font-size: 13px;
    font-weight: 500;
    color: #FF3F15;
    flex-shrink: 0;
    line-height: 38px;
    margin-left: 10px;
  }

  .successBox{
    background: #fff;
    width: 100vw;
    height: calc(100vh - 53px);
    position: fixed;
    left: 0;
    top: 53px;
    display: flex;
    flex-direction: column;
  }
  .successInner{
    margin-top: 100px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 500;
    color: #5D5D5D;
  }
  .successIcon{
    width: 64px;
  }
  .successWord{
    font-size: 18px;
    font-weight: bold;
    color: #313131;
    margin: 30px 0 8px;
  }
  .successButton{
    margin-top: 50px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
  }
  .successGo{
    background: url("../../assets/images/jobRegister-bg.jpg") repeat 0 0;
    background-size: 100%;
    border-radius: 50px;
    line-height: 1;
    padding: 12px 36px;
    font-size: 14px;
    font-weight: 500;
    color: #FFFFFF;
    margin-right: 16px;
    display: block;
  }
  .successHome{
    display: block;
    border:1px solid #FF3F15;
    border-radius: 50px;
    line-height: 1;
    padding: 12px 36px;
    font-size: 14px;
    font-weight: 500;
    color: #FF3F15;
  }
  .successCode{
    position: absolute;
    left: 0;
    right: 0;
    bottom: 48px;
    margin: auto;
    width: 312px;
    height: 110px;
    background: #FFFFFF;
    box-shadow: 0px 0px 11px 0px rgba(255, 28, 38, 0.07);
    border-radius: 5px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;

  }
  .successCodeImg{
    width: 80px;
    margin-right: 16px;
    border: 1px solid rgba(237, 237, 237, 0.99);
  }
  .successCodeWord{
    display: flex;
    flex-direction: column;
    font-size: 14px;
    font-weight: 500;
    color: #5D5D5D;
    .words{
      margin-bottom: 12px;
      color: #393939;

    }
  }
  .loading{
    display: flex;
    justify-content: center;
    align-items: center;
    height: 50vh
  }
</style>
<style>
  .vanForm .van-checkbox__icon{
    height: auto;
  }
</style>
